//全部商机

const router = require('koa-router')()
const { qunfa } = require('../model')

// 添加数据
router.post('/qunfa/add', async (ctx, next) => {
  const {name,customerName,tel,num,state,establishTime,updateTime} = ctx.request.body
  const res = await qunfa.create({name,customerName,tel,num,state,establishTime,updateTime})
  if (res) {
    ctx.body = {
      code: 200,
      msg: '添加成功',
      name,customerName,tel,num,state,establishTime,updateTime
    }
  }
})

// 获取全部数据
router.post('/qunfa/getAll', async (ctx, next) => {
  const res = await qunfa.find();
  console.log(res);
  if (res) {
    ctx.body = {
      code: 200,
      msg: '获取全部数据成功',
      data: res
    }
  }
})

// 修改数据
router.post('/qunfa/update', async (ctx, next) => {
  const {ids,name,customerName,tel,num,state,establishTime,updateTime } = ctx.request.body
  let res = await qunfa.updateOne(
    { _id: ids },
    {name,customerName,tel,num,state,establishTime,updateTime} 
  )
  if (res) {
    ctx.body = {
      code: 200,
      msg: '修改成功',
      data: res
    }
  }
})

//删除
router.post('/qunfa/del', async (ctx, next) =>{
  const { id } = ctx.request.body
  const res = await qunfa.deleteOne({ _id: id })

  if (res) {
      ctx.body = {
          code: 200,
          msg: '删除成功',
          data: res
      }
  }
})

module.exports = router